#include "tcpsocket.h"

TcpSocket::TcpSocket(QObject *parent)
{
    // 当客户端断开时，执行槽函数
    connect(this, &QAbstractSocket::disconnected, this, &TcpSocket::clientDisconnectedSlot);
    // 当收到消息时，执行槽函数
    connect(this, &QIODevice::readyRead, this, &TcpSocket::msgReceivedSlot);
}

TcpSocket::~TcpSocket()
= default;

// 客户端断开的槽函数
void TcpSocket::clientDisconnectedSlot()
{
    // 发送信号到服务端
    emit clientDisconnected(this->socketDescriptor());
    // qDebug() << this->socketDescriptor();
}
// 收到消息的槽函数
void TcpSocket::msgReceivedSlot()
{
    // 将消息写入缓冲区
    QByteArray datagram = this->readAll();
    // 发送信号到服务端
    emit msgReceived(datagram, this->socketDescriptor());
}
